//
//  DDropsCustomEndTimeView.swift
//  Drop
//
//  Created by hushaohua on 2018/1/28.
//  Copyright © 2018年 init. All rights reserved.
//

import UIKit

class DDropSlider: UISlider {
    override func thumbRect(forBounds bounds: CGRect, trackRect rect: CGRect, value: Float) -> CGRect {
        return super.thumbRect(forBounds: bounds, trackRect: rect, value: Float(Int(value)));
    }
}

class DDropsCustomEndTimeView: HMaskContentVIew {

    override init(frame: CGRect) {
        super.init(frame: frame);
        self.addSubview(self.topBarView);
        self.topBarView.addSubview(self.tagLabel);
        
        self.addSubview(self.beginLabel);
        self.addSubview(self.currentLabel);
        self.addSubview(self.timeLabel);
        
        self.addSubview(self.beginTitleLabel);
        self.addSubview(self.currentTitleLabel);
        self.addSubview(self.timeTitleLabel);
        
        self.addSubview(self.timeSlider);

        self.addSubview(self.okButton);
        
        self.addSubviewLayoutConstraints();
    }
    
    lazy var slider:DDropTimeSlider = {
        var slider = DDropTimeSlider(frame: CGRect(x:0, y:54, width:self.bounds.size.width, height:80));
        slider.backgroundColor = UIColor.yellow;
        slider.translatesAutoresizingMaskIntoConstraints = false;
        return slider;
    }();
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var topBarView:UIView = {
        var view:UIView = UIView(frame: CGRect(x: 0, y: 0, width: self.bounds.width, height: 44));
        view.backgroundColor = UIColor(white:0.95,alpha:1);
        return view;
    }();
    
    var mTag:Tag?;
    var drop:Drop?;
    func show(drop:Drop, at tag:Tag) {
        self.mTag = tag;
        self.drop = drop;
        self.showWithMask();
        
        self.tagLabel.text = self.mTag?.name;
        let seconds:Int = Int(Date().timeIntervalSince(self.drop!.begin! as Date));
        let minutes:Int = seconds / 60;
        self.timeSlider.minimumValue = 0;
        self.timeSlider.maximumValue = Float(minutes);
        self.timeSlider.value = 0;
        
        self.resetBeginLabel(drop.begin! as Date);
        self.currentLabel.text = "0'";
        self.timeLabel.text = seconds.timeString();
    }
    
    func resetBeginLabel(_ date:Date) -> Void{
        let dateFormatter:DateFormatter = DateFormatter();
        if Calendar.current.isDateInToday(date) {
            dateFormatter.dateFormat = "HH:mm";
        }else{
            dateFormatter.dateFormat = "EEE HH:mm";
        }
        self.beginLabel.text = dateFormatter.string(from: date);
    }
    
    @objc func timeSliderChanged(_ slider:UISlider) -> Void{
        let seconds:Int = Int(slider.value * 60);
        let date = Date(timeInterval: TimeInterval(slider.value * 60), since: self.drop!.begin! as Date);
        let dateFormatter:DateFormatter = DateFormatter();
        if Calendar.current.isDateInToday(date) {
            dateFormatter.dateFormat = "HH:mm";
        }else{
            dateFormatter.dateFormat = "EEE HH:mm";
        }
        let string = dateFormatter.string(from: date);
        self.currentLabel.text = "\(seconds.timeString())(\(string))";
    }
    
    override func showWithMask() {
        super.showWithMask();
        self.sharedWindowManager()?.window.backgroundColor = UIColor.clear;
    }
    
    lazy var tagLabel:UILabel = {
        var label:UILabel = UILabel(frame: CGRect(x: 20, y: 0, width: self.bounds.width - 40, height: 44));
        label.font = UIFont.systemFont(ofSize: 18, weight: .medium);
        return label;
    }();
    
    lazy var beginLabel:UILabel = {
        var label:UILabel = UILabel(frame: .zero);
        label.translatesAutoresizingMaskIntoConstraints = false;
        label.textColor = UIColor.lightGray;
        label.adjustsFontSizeToFitWidth = true;
        label.text = "开始";
        return label;
    }();
    lazy var timeLabel:UILabel = {
        var label:UILabel = UILabel(frame: .zero);
        label.translatesAutoresizingMaskIntoConstraints = false;
        label.textColor = UIColor.darkGray;
        label.textAlignment = .right;
        label.adjustsFontSizeToFitWidth = true;
        return label;
    }();
    lazy var currentLabel:UILabel = {
        var label:UILabel = UILabel(frame: .zero);
        label.translatesAutoresizingMaskIntoConstraints = false;
        label.textColor = UIColor.h_blue();
        label.textAlignment = .center;
        return label;
    }();
    
    lazy var beginTitleLabel:UILabel = {
        var label:UILabel = UILabel(frame: .zero);
        label.translatesAutoresizingMaskIntoConstraints = false;
        label.textColor = UIColor(white: 0.8, alpha: 1);
        label.font = UIFont.italicSystemFont(ofSize: 12)
        label.text = "Begin";
        return label;
    }();
    lazy var timeTitleLabel:UILabel = {
        var label:UILabel = UILabel(frame: .zero);
        label.translatesAutoresizingMaskIntoConstraints = false;
        label.textColor = UIColor(white: 0.8, alpha: 1);
        label.textAlignment = .right;
        label.font = UIFont.italicSystemFont(ofSize: 12)
        label.text = "总时长";
        return label;
    }();
    lazy var currentTitleLabel:UILabel = {
        var label:UILabel = UILabel(frame: .zero);
        label.translatesAutoresizingMaskIntoConstraints = false;
        label.textColor = UIColor(white: 0.8, alpha: 1);
        label.textAlignment = .center;
        label.font = UIFont.italicSystemFont(ofSize: 12)
        label.text = "耗时(截止时间)";
        return label;
    }();
    
    func addSubviewLayoutConstraints() {
        let views = ["begin" : self.beginLabel, "time" : self.timeLabel, "current" : self.currentLabel, "beginT" : self.beginTitleLabel, "timeT" : self.timeTitleLabel, "currentT" : self.currentTitleLabel, "slider" : self.timeSlider, "ok" : self.okButton, "super" : self];
        var constraints = [NSLayoutConstraint]();
        constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[begin]-[current]-[time]-20-|", options: [.alignAllTop, .alignAllBottom], metrics: nil, views: views);
        constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[beginT]-[currentT]-[timeT]-20-|", options: [.alignAllTop, .alignAllBottom], metrics: nil, views: views);
        constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-(-3)-[slider]-(-3)-|", options: [.directionLeadingToTrailing], metrics: nil, views: views);
        constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:[ok(==160)]", options: [.directionLeadingToTrailing], metrics: nil, views: views);
        constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-44-[currentT(==20)]-0-[current(==20)]-10-[slider(==40)]-30-[ok(==40)]", options: [.alignAllCenterX], metrics: nil, views: views);
        NSLayoutConstraint.activate(constraints);
    }
    
    func addOkConstraints() {
        let views = ["slider" : self.timeSlider, "ok" : self.okButton, "super" : self];
        var constraints = [NSLayoutConstraint]();
        constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-80-[ok]-80-|", options: [.directionLeadingToTrailing], metrics: nil, views: views);
        constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[slider]-0-|", options: [.directionLeadingToTrailing], metrics: nil, views: views);
        constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-54-[slider(==80)]-30-[ok(==40)]", options: [.alignAllCenterX], metrics: nil, views: views);
        NSLayoutConstraint.activate(constraints);
    }
    
    lazy var timeSlider:DDropSlider = {
        var slider = DDropSlider(frame: CGRect(x: -3, y: 94, width: self.bounds.width + 6, height: 40));
        slider.translatesAutoresizingMaskIntoConstraints = false;
        let thumbImage = UIImage.imageFrom(UIColor.h_blue(), size: CGSize(width:20,height:40));
        slider.setThumbImage(thumbImage, for: .normal);
        let miniImage = UIImage.imageFrom(UIColor.h_blue(), size: CGSize(width:1,height:40));
        let maxImage = UIImage.imageFrom(UIColor.h_lightblue(), size: CGSize(width:self.bounds.width,height:40));
        slider.setMaximumTrackImage(maxImage, for: .normal);
        slider.setMinimumTrackImage(miniImage, for: .normal);
        slider.addTarget(self, action: #selector(timeSliderChanged(_:)), for: .valueChanged);
        return slider;
    }();
    
    lazy var okButton:UIButton = {
        var button:UIButton = UIButton(frame: CGRect(x: (self.bounds.size.width - 160) / 2, y: 164, width: 160, height: 40));
        button.translatesAutoresizingMaskIntoConstraints = false;
        button.setTitleColor(UIColor.h_blue(), for: .normal);
        button.setTitle("确定", for: .normal);
        button.layer.borderColor = UIColor.h_blue().cgColor;
        button.layer.borderWidth = 1;
        button.addTarget(self, action: #selector(okButtonClicked(_:)), for: .touchUpInside);
        return button;
    }();
    
    var okClosure:((Int) -> Void)?;
    @objc func okButtonClicked(_ sender:UIButton) -> Void{
        if (self.okClosure != nil){
            self.okClosure!(Int(self.timeSlider.value));
        }
    }

}
